import http from "@/utils/http";
import { HttpResponse, checkHttpError } from "./common";
import { FORM_DATA_AXIOS_CONFIG } from "./constants";

/**
 * 获得日志列表
 *
 * @param page
 * @param size
 * @param keyword
 * @param importance
 * @param startTime
 * @param endTime
 * @returns
 */
export async function getSysLogList(
  page: number,
  size: number,
  keyword: string,
  importance: string = "",
  startTime: string = "",
  endTime: string = ""
) {
  const queryParams = `page=${page}&search=${keyword}&page_size=${size}&importance=${importance}&dt_ins_gte=${startTime}&dt_ins_lte=${endTime}`;

  // 这个很特别...不是那种status和detail的响应结构
  const resp: LogResponse = await http.get(
    "/Helpsyslog/listsyslog/?" + queryParams
  );

  return resp;
}

/**
 * 系统操作记录
 * @param {Operation} opare 操作类型
 * @param {string} batch_eqs 批量设备
 * @param {string} menu1 一级菜单
 * @param {string} menu2 二级菜单
 * @param {string} menu3 三级菜单
 * @returns
 */
export async function recordOperateLog(
  opare: Operation,
  menu1 = "",
  menu2 = "",
  menu3 = "",
  batch_eqs = ""
) {
  const form = new FormData();

  form.append("menu1", menu1);
  form.append("menu2", menu2);
  form.append("menu3", menu3);
  form.append("batch_eqs", batch_eqs);
  form.append("method_button", opare.name);
  form.append("importance", opare.importance + "");

  const resp: HttpResponse = await http.post(
    "/Helpsyslog/addsyslog/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail;
}

/**
 * 生成固定一二级菜单的系统操作记录函数
 * 只是为了方便
 */
export function recordOperateLogFunc(
  menu1: string,
  menu2: string,
  menu33: string = ""
) {
  return (opare: Operation, menu3 = menu33, batch_eqs = "") =>
    recordOperateLog(opare, menu1, menu2, menu3, batch_eqs);
}

export const OperationType = Object.freeze({
  // 高重要性
  Add: { name: "新增", importance: 1 },
  Delete: { name: "删除", importance: 1 },
  QuickExec: { name: "立即执行", importance: 1 },
  TimerExec: { name: "定时执行", importance: 1 },
  Disable: { name: "禁用", importance: 1 },
  Enable: { name: "启用", importance: 1 },
  Send: { name: "发送", importance: 1 },
  Upload: { name: "上传", importance: 1 },
  ReUpload: { name: "重传", importance: 1 },
  RunScript: { name: "运行脚本", importance: 1 },
  DeleteScript: { name: "删除脚本", importance: 1 },
  Backup: { name: "备份", importance: 1 },
  TemplateAdd: { name: "模版新增", importance: 1 },
  TemplateDelete: { name: "模版删除", importance: 1 },
  StepAdd: { name: "步骤新增", importance: 1 },
  StepDelete: { name: "步骤删除", importance: 1 },
  OperaObjectAdd: { name: "操作对象新增", importance: 1 },
  OperaObjectDelete: { name: "操作对象删除", importance: 1 },
  RunUpgradeTask: { name: "执行升级任务", importance: 1 },
  Copy: { name: "复制", importance: 1 },
  BatchUpgrade: { name: "批量升级", importance: 1 },
  AddTmpEq: { name: "新增临时设备", importance: 1 },
  RecordEq: { name: "录入设备", importance: 1 },
  MallocUser: { name: "分配用户", importance: 1 },
  MallocPermission: { name: "分配权限", importance: 1 },
  MallocCommand: { name: "分配命令", importance: 1 },
  MallocEq: { name: "分配设备", importance: 1 },
  MallocCommandGroup: { name: "分配命令组", importance: 1 },
  MallocEqGroup: { name: "分配设备组", importance: 1 },
  MallocSystem: { name: "分配系统", importance: 1 },
  AddIP: { name: "新增IP", importance: 1 },
  DeleteIP: { name: "删除IP", importance: 1 },
  AddEnv: { name: "新增环境", importance: 1 },
  DeleteEnv: { name: "删除环境", importance: 1 },
  AddAL: { name: "新增架构分层", importance: 1 },
  DeleteAL: { name: "删除架构分层", importance: 1 },
  AddNetworkCard: { name: "新增网卡", importance: 1 },
  DeleteNetworkCard: { name: "删除网卡", importance: 1 },
  ReUpgrade: { name: "重新升级", importance: 1 },
  BatchUpdate: { name: "批量变更", importance: 1 },
  Execute: { name: "执行", importance: 1 },
  Run: { name: "运行", importance: 1 },
  UpdateEq: { name: "更新设备", importance: 1 },
  Import: { name: "导入", importance: 1 },
  ConfirmBack: { name: "确认返回", importance: 1 },
  ImportProcess: { name: "导入进程", importance: 1 },
  Close: { name: "关闭", importance: 1 },
  Open: { name: "开启", importance: 1 },
  Install: { name: "安装", importance: 1 },
  Uninstall: { name: "卸载", importance: 1 },
  Back: { name: "退回", importance: 1 },
  AddUserGroup: { name: "新增用户组", importance: 1 },
  NoMonitor: { name: "不必监控", importance: 1 },
  ToMonitor: { name: "纳入监控", importance: 1 },
  Sync: { name: "同步", importance: 1 },

  // 中重要性
  Update: { name: "修改", importance: 2 },
  Publish: { name: "发布", importance: 2 },
  CancelPublish: { name: "取消发布", importance: 2 },
  CheckDownload: { name: "检验下载", importance: 2 },
  TemplateUpdate: { name: "模版修改", importance: 2 },
  StepUpdate: { name: "步骤修改", importance: 2 },
  OpareObjectUpdate: { name: "操作对象修改", importance: 2 },
  ImportEqGroup: { name: "导入设备组", importance: 2 },
  UpdateIP: { name: "修改IP", importance: 2 },
  IPSegmentScan: { name: "IP段扫描", importance: 2 },
  GetInformation: { name: "获取信息", importance: 2 },
  UpdateNetworkCard: { name: "修改网卡", importance: 2 },
  Rename: { name: "重命名", importance: 2 },
  Scan: { name: "扫描", importance: 2 },
  ParesSelect: { name: "解析查询", importance: 2 },
  ProcessScan: { name: "进程扫描", importance: 2 },
  Unlock: { name: "解锁", importance: 2 },

  // 低重要性
  Select: { name: "查询", importance: 3 },
  Detail: { name: "详情", importance: 3 },
  Export: { name: "导出", importance: 3 },
  SelectIP: { name: "查询ip", importance: 3 },
  ResultDetail: { name: "结果详情", importance: 3 },
  ExportResult: { name: "导出结果", importance: 3 },
  BrowseDir: { name: "浏览目录", importance: 3 },
  TemplateDetail: { name: "模版详情", importance: 3 },
  StepDetail: { name: "步骤详情", importance: 3 },
  OpareObjectDetail: { name: "操作对象详情", importance: 3 },
  TaskDetail: { name: "任务详情", importance: 3 },
  UpgradeResultDetail: { name: "升级结果详情", importance: 3 },
  UserGroup: { name: "用户组", importance: 3 },
  Role: { name: "角色", importance: 3 },
  EqGroup: { name: "设备组", importance: 3 },
  IP: { name: "ip", importance: 3 },
  Environment: { name: "环境", importance: 3 },
  AL: { name: "架构分层", importance: 3 },
  NetworkCard: { name: "网卡", importance: 3 },
  UpgradeResultSelect: { name: "升级结果查询", importance: 3 },
  StepDetailInfo: { name: "步骤明细", importance: 3 },
  SelectEQGroup: { name: "查询设备组", importance: 3 },
  SelectCluster: { name: "查询集群", importance: 3 },
  SelectAL: { name: "查询架构分层", importance: 3 },
  BrowseScript: { name: "浏览脚本", importance: 3 },
  SelectRole: { name: "查询角色", importance: 3 },
  SelectUserGroup: { name: "查询用户组", importance: 3 }
});

export declare interface Operation {
  name: string;
  importance: number;
}

export interface LogResponse {
  count: number;
  next: string;
  previous: string;
  results: LogItem[];
}

/**
 * 单条目日志结构
 */
export interface LogItem {
  // 批量设备
  batch_eqs: string;
  // 部门信息
  deptid: object;
  // 发生时间
  dt_ins: string;
  // 用户信息
  empid: any;
  id: number;
  importance: number;
  menu1: string;
  menu2: string;
  menu3: string;
  method_button: string;
  remark: string;
  userip: string;

  shortcpname: object;
}
